package kevin.hbase.config;

import kevin.hbase.exception.CommonException;
import kevin.hbase.common.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.util.List;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public HttpResult exception(Exception e) {
        log.error("系统异常: ", e);
        return HttpResult.failed();
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public HttpResult httpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("不合法参数", e);
        return HttpResult.failed(HttpResult.Status.PARAMS_VALID_ERROR.code, HttpResult.Status.PARAMS_VALID_ERROR.msg);
    }

    /**
     * 对实体类中@NotNull等注解异常进行捕获
     *
     * @param e MethodArgumentNotValidException
     * @return Result 返回结果实体类
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public HttpResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        StringBuilder errorInfo = new StringBuilder();
        for (ObjectError error : errors) {
            errorInfo.append(error.getDefaultMessage()).append(";");
        }
        log.error("参数验证失败: {}", errorInfo.toString(), e);
        return HttpResult.failed(HttpResult.Status.PARAMS_VALID_ERROR.code, HttpResult.Status.PARAMS_VALID_ERROR.msg + ";" + errorInfo);
    }

    /**
     * 对实体类中@NotNull等注解异常进行捕获
     *
     * @param e MethodArgumentNotValidException
     * @return Result 返回结果实体类
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    public HttpResult bindException(BindException e) {
        List<ObjectError> errors = e.getBindingResult().getAllErrors();
        StringBuilder errorInfo = new StringBuilder();
        for (ObjectError error : errors) {
            errorInfo.append(error.getDefaultMessage()).append(";");
        }
        log.error("参数验证失败: {}", errorInfo.toString(), e);
        return HttpResult.failed(HttpResult.Status.PARAMS_VALID_ERROR.code, HttpResult.Status.PARAMS_VALID_ERROR.msg + ";" + errorInfo);
    }

    /**
     * 对超过文件大小异常进行捕获
     *
     * @param e MaxUploadSizeExceededException
     * @return Result 返回结果实体类
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public HttpResult maxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        log.error("文件大小不能超过100MB", e);
        return HttpResult.requiredParamBlank();
    }

    /**
     * 对请求方式错误异常进行捕获
     *
     * @param e HttpRequestMethodNotSupportedException
     * @return Result 返回结果实体类
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public HttpResult maxUploadSizeExceededException(HttpRequestMethodNotSupportedException e) {
        log.error("请求方式错误", e);
        return HttpResult.failed(HttpResult.Status.REQ_METHOD_ERROR.code, HttpResult.Status.REQ_METHOD_ERROR.msg);
    }

    /**
     * 对通用自定义异常进行捕获
     *
     * @param e CommonException
     * @return Result 返回结果实体类
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(CommonException.class)
    public HttpResult maxUploadSizeExceededException(CommonException e) {
        log.error(e.getMessage(), e);
        return HttpResult.failed(e.getMessage());
    }

}